package com.atguigu.tingshu.payment.strategy.impl;

import com.atguigu.tingshu.account.client.RechargeInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.payment.config.WxPayV3Config;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.strategy.PaymentStrategy;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.payment.WeChatPayVo;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class RechargePaymentStrategy implements PaymentStrategy  {

    @Autowired
    private RechargeInfoFeignClient rechargeInfoFeignClient ;

    @Autowired
    private PaymentInfoService paymentInfoService ;

    @Autowired
    private RSAAutoCertificateConfig rsaAutoCertificateConfig ;

    @Autowired
    private WxPayV3Config wxPayV3Config ;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient ;

    @Override
    public WeChatPayVo createJsapi(String paymentType, String orderNo) {

        // 根据订单编号查询充值记录表中的订单数据
        Result<RechargeInfo> rechargeInfoResult = rechargeInfoFeignClient.getByOrderNo(orderNo);
        RechargeInfo rechargeInfo = rechargeInfoResult.getData();
        if(SystemConstant.ORDER_STATUS_CANCEL.equals(rechargeInfo.getRechargeStatus())) {
            return null;
        }

        // 保存交易信息
        paymentInfoService.savePaymentInfo(rechargeInfo);

        WeChatPayVo weChatPayVo = this.requestWeChatPay(rsaAutoCertificateConfig, wxPayV3Config,
                "充值", rechargeInfo.getOrderNo(), userInfoFeignClient);

        return weChatPayVo;
    }

    @Override
    public String getPaymentType() {
        return SystemConstant.PAYMENT_TYPE_RECHARGE;  // 1302
    }
}
